package com.longxi.tianjintianbao.exception;

import android.util.Log;

import com.google.gson.JsonParseException;
import com.longxi.tianjintianbao.utils.LogUtils;

import org.greenrobot.greendao.DaoException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import retrofit2.HttpException;

/**
 * Created by cxw on 2018/3/23.
 */

public class ExceptionHandle {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ResponeThrowable handleException(Throwable e) {
        ResponeThrowable ex;
        LogUtils.i("tag", "e.toString = " + e.toString());
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ResponeThrowable(e, ERROR.HTTP_ERROR);
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    //ex.code = httpException.code();
                    ex.message = "未知错误";
                    break;
            }
            return ex;
        } else if (e instanceof ServerException) {
            ServerException resultException = (ServerException) e;
            ex = new ResponeThrowable(resultException, resultException.code);
            ex.message = resultException.message;
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
            /*|| e instanceof ParseException*/) {
            ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
            ex.message = "解析错误";
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
            ex.message = "连接失败";
            return ex;
        } else if (e instanceof SocketTimeoutException) {
            ex = new ResponeThrowable(e, ERROR.SOCKET_TIMEOUT);
            ex.message = "服务器繁忙";
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else if (e instanceof DaoException) {
            ex = new ResponeThrowable(e, ERROR.DAOGREEN_ERROR);
            ex.message = "本地数据库异常";
            return ex;
        } else if (e instanceof NullPointerException) {
            ex = new ResponeThrowable(e, ERROR.NULL_POINTER_EXCEPTION);
//            ex.message = "空指针异常";
            ex.message = "本地数据错误，请重启APP";
            return ex;
        } else {
            ex = new ResponeThrowable(e, ERROR.UNKNOWN);
//            ex.message = "未知错误";
            ex.message = "本地数据错误，请重启APP";
            LogUtils.d("ExceptionHandle", ex.message);
            LogUtils.d("ExceptionHandle", e.toString());
            return ex;
        }
    }


    /**
     * 约定异常
     */
    public class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 网络错误
         */
        public static final int NETWORD_ERROR = 1002;
        /**
         * 协议出错
         */
        public static final int HTTP_ERROR = 1003;

        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1005;
        /**
         * 连接超时
         */
        public static final int SOCKET_TIMEOUT = 1006;
        /**
         * 连接超时
         */
        public static final int DAOGREEN_ERROR = 1007;

        /**
         * 空指针异常
         */
        public static final int NULL_POINTER_EXCEPTION = 1008;

        /**
         * 用户名或密码错误
         */
        public static final int USERNAME_PASSWORD_ERROR = 101;

        /**
         * 签名错误
         */
        public static final int SIGNATURE_ERROR = 901;

        /**
         * 方法参数不正确
         */
        public static final int METHOD_PARAMETER_ERROR = 902;

        /**
         * 数据参数不正确
         */
        public static final int DATA_PARAMETER_ERROR = 903;

        /**
         * 系统错误
         */
        public static final int SYSTEM_ERROR = 904;
    }

    public static class ResponeThrowable extends Exception {
        public int code;
        public String message;

        public ResponeThrowable(Throwable throwable, int code) {
            super(throwable);
            this.code = code;
        }
    }

    /**
     * ServerException发生后，将自动转换为ResponeThrowable返回
     */
    class ServerException extends RuntimeException {
        int code;
        String message;
    }

}
